{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "f78ccf47",
   "metadata": {},
   "source": [
    "*记录人：温倩雅\n",
    "*时间：2021-11-15\n",
    "*week12\n",
    "*学习线上资源：[python官方文档]（https://python.org）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "d3a6d491",
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid syntax (<ipython-input-6-c0340c67ec63>, line 3)",
     "output_type": "error",
     "traceback": [
      "\u001b[0;36m  File \u001b[0;32m\"<ipython-input-6-c0340c67ec63>\"\u001b[0;36m, line \u001b[0;32m3\u001b[0m\n\u001b[0;31m    > 1.字典的查询  查询key -> value\u001b[0m\n\u001b[0m    ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n"
     ]
    }
   ],
   "source": [
    "##本周知识点\n",
    "###1.字典的一般方法\n",
    "> 1.字典的查询  查询key -> value\n",
    "> 2.字典的修改\n",
    "> 3.字典的删除\n",
    "> 4.字典的增加（ dict_name[key]=value ）\n",
    "\n",
    "###2.字典的进阶方法\n",
    "\n",
    "> 1.字典的初始化\n",
    ">> 1. D.fromkrys()\n",
    ">> 2. D.setdefault()\n",
    "\n",
    "###3.字典的时间（一）课本\n",
    "###4.字典的时间（二）课本\n",
    "###5.字典的时间（三）课外"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ae4f8637",
   "metadata": {},
   "source": [
    "##  1.字典的一般方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "d1a6fe45",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'stu1801': 'ZiQi', 'stu2021': 'Qianya', 'stu2001': 'Sitong'}\n"
     ]
    }
   ],
   "source": [
    "stu_info = {\n",
    "    'stu1801':\"ZiQi\",\n",
    "    'stu2021':'Qianya',\n",
    "    'stu2001':'Sitong'\n",
    "}\n",
    "print(stu_info)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "ded77774",
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid syntax (<ipython-input-14-19c24c14fdad>, line 1)",
     "output_type": "error",
     "traceback": [
      "\u001b[0;36m  File \u001b[0;32m\"<ipython-input-14-19c24c14fdad>\"\u001b[0;36m, line \u001b[0;32m1\u001b[0m\n\u001b[0;31m    >1. 字典的查询\u001b[0m\n\u001b[0m    ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n"
     ]
    }
   ],
   "source": [
    ">1. 字典的查询"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "2ca5e3a8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Qianya'"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 直接查找 dict_name[key]\n",
    "stu_info['stu2021']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "61db90ff",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function get:\n",
      "\n",
      "get(key, default=None, /) method of builtins.dict instance\n",
      "    Return the value for key if key is in the dictionary, else default.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "#使用get方法查找\n",
    "help(c)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "0bce42be",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "None\n"
     ]
    }
   ],
   "source": [
    "print(stu_info.get('stu2022'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "9cf24ecc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Qianya'"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stu_info.get('stu2021')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "2e25ce99",
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid character in identifier (<ipython-input-23-06c078865cbe>, line 1)",
     "output_type": "error",
     "traceback": [
      "\u001b[0;36m  File \u001b[0;32m\"<ipython-input-23-06c078865cbe>\"\u001b[0;36m, line \u001b[0;32m1\u001b[0m\n\u001b[0;31m    〉2. 修改\u001b[0m\n\u001b[0m     ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid character in identifier\n"
     ]
    }
   ],
   "source": [
    "〉2. 修改 & 新增\n",
    "〉〉1. 先查询  查看key是否存在。使用in查询成员\n",
    "〉〉2.如果key存在。修改"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "3d461075",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#查询 stu2021\n",
    "'stu2021'in stu_info"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "4427a6a6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'stu1801': 'Sitong', 'stu2021': 'Sitong', 'stu2001': 'Sitong'}"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#如果key存在 修改\n",
    "if 'stu2021' in stu_info:\n",
    "    stu_info['stu1801'] = 'Sitong'\n",
    "stu_info"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e1722bc7",
   "metadata": {},
   "outputs": [],
   "source": [
    "> 3. 字典的删除\n",
    ">>1.D.POP()\n",
    ">>2.D.popitem()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "4bc99a34",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function pop:\n",
      "\n",
      "pop(...) method of builtins.dict instance\n",
      "    D.pop(k[,d]) -> v, remove specified key and return the corresponding value.\n",
      "    If key is not found, d is returned if given, otherwise KeyError is raised\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(stu_info.pop)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a6a0ed3e",
   "metadata": {},
   "outputs": [],
   "source": [
    "stu_info.pop()  #  不正确，不同于列表（删除最后一个）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "bedf9a81",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 字典删除的正确使用\n",
    "if 'stu2021' in stu_info:\n",
    "    stu_info.pop('stu2021')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "467e0a69",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'stu1801': 'Sitong', 'stu2001': 'Sitong'}"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stu_info"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "2133aacc",
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "'dict' object is not callable",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-35-2dd1c7d26b2a>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mstu_info\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'stu1000'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m: 'dict' object is not callable"
     ]
    }
   ],
   "source": [
    "#如果不先查询 key是否存在，有可能报错：keyerror:'stu10000'\n",
    "stu_info('stu1000')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "845891ff",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function popitem:\n",
      "\n",
      "popitem() method of builtins.dict instance\n",
      "    Remove and return a (key, value) pair as a 2-tuple.\n",
      "    \n",
      "    Pairs are returned in LIFO (last-in, first-out) order.\n",
      "    Raises KeyError if the dict is empty.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(stu_info.popitem)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "0000fa0c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('stu2001', 'Sitong')"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stu_info.popitem()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "ef186082",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'stu1801': 'Sitong'}"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stu_info"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "1d2edcb4",
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'word' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-42-83d081d86da4>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m      4\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mi\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mvowels\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      5\u001b[0m     \u001b[0mfound\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mi\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 6\u001b[0;31m \u001b[0;32mfor\u001b[0m \u001b[0mi\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mword\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m      7\u001b[0m     \u001b[0;32mif\u001b[0m \u001b[0mi\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mviwels\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      8\u001b[0m         \u001b[0mfound\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mi\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m+=\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mNameError\u001b[0m: name 'word' is not defined"
     ]
    }
   ],
   "source": [
    "## 目标：查询。元音字母是否出现在word文档中，并统计出现的次数、、\n",
    "vowels = [\"a\",\"e\",\"i\",\"o\",\"u\"]\n",
    "found = {}\n",
    "for i in vowels:\n",
    "    found[i] = 0\n",
    "for i in word:\n",
    "    if i in viwels:\n",
    "        found[i] +=1\n",
    "found"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "8e925c07",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function fromkeys:\n",
      "\n",
      "fromkeys(iterable, value=None, /) method of builtins.type instance\n",
      "    Create a new dictionary with keys from iterable and values set to value.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(found.fromkeys)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b17f4ae3",
   "metadata": {},
   "outputs": [],
   "source": [
    "vowels = ['a','e','i','o','u']\n",
    "word = input('Provide a word to search for vowels:')\n",
    "found = {}\n",
    "#初始化 字典\n",
    "found = found.fromkeys(vowels,10)\n",
    "for i in word:\n",
    "    if i in vowels:\n",
    "        found[i] +=1\n",
    "found"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "656f97be",
   "metadata": {},
   "source": [
    ">字典的进阶犯法？初始化特定的值 setdefault()\n",
    ">>*查询key是否出现在字典当中\n",
    ">>>1.出现过，就当没看见，不做任何操作\n",
    ">>>2.key从未出现过，给定key及value(指定的初始化的值)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "f6e0dfcf",
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'found' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-1-0bc0db48ae16>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mhelp\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mfound\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msetdefault\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;31mNameError\u001b[0m: name 'found' is not defined"
     ]
    }
   ],
   "source": [
    "help(found.setdefault)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "dbbe170c",
   "metadata": {},
   "outputs": [],
   "source": [
    "stu_password = {\n",
    "    'Xiaoing':'2223333'\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "5ab9c15e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Provide a word to search for vowels:wenqianya\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'a': 12, 'e': 11, 'i': 11, 'o': 10, 'u': 10}"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "##优化实践（一）\n",
    "vowels = ['a','e','i','o','u']\n",
    "word = input('Provide a word to search for vowels:')\n",
    "found = {}\n",
    "#初始化 字典\n",
    "found = found.fromkeys(vowels,10)\n",
    "for i in word:\n",
    "    if i in vowels:\n",
    "        found.setdefault(i,0)\n",
    "        found[i] +=1\n",
    "found"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d87510f1",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e884ca91",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
